#!/bin/bash

main() {
  nginx

  do_chown

  cd /srv/mediagoblin.example.org/mediagoblin/

  do_update

  if [ ! -f /var/lib/mediagoblin/date_configured ]
    then
      do_config
  fi

  startup
}

startup() {
  sudo -u mediagoblin ./lazyserver.sh --server-name=fcgi fcgi_host=127.0.0.1 fcgi_port=26543
}

do_update() {
  sudo -u mediagoblin bin/gmg dbupdate
}

do_config() {
  sudo -u mediagoblin bin/gmg adduser --username $GOBLIN_USER --password $GOBLIN_PASSWORD --email $GOBLIN_EMAIL
  sudo -u mediagoblin bin/gmg makeadmin $GOBLIN_USER
  date -I > /var/lib/mediagoblin/date_configured
}

do_chown() {
  chown -hR mediagoblin:www-data /var/lib/mediagoblin
}

main "$@"
